package com.bwie.controller;

import com.bwie.bean.City;
import com.bwie.bean.Course;
import com.bwie.bean.Stu;
import com.bwie.service.StuService;
import com.bwie.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StuController {

    @Autowired
    private StuService stuService;

    @RequestMapping("list.do")
    public String list(

            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            String sname,
            String birs,
            String birl,
            String sex,
            Integer sheng,
            Integer shi,
            Integer xian,
            Model model){

        Map map = new HashMap();
        map.put("sname",sname);
        map.put("birs",birs);
        map.put("birl",birl);
        map.put("sex",sex);
        map.put("sheng",sheng);
        map.put("shi",shi);
        map.put("xian",xian);

        PageHelper.startPage(pageNum,3);
        List list=stuService.list(map);
        PageInfo pageInfo = new PageInfo(list);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }


    //动态三级联动
    @RequestMapping("listcity.do")
    @ResponseBody
    //查询根据pid  查询的是集合
    public List<City> listCity(Integer pid){

         List<City> c=stuService.listCity(pid);
         return c;
    }

    //删除
    @RequestMapping("delstu.do")
    @ResponseBody
    public Object delstu(Integer sid){
        int i=stuService.delstu(sid);
        return i;
    }

    //添加
    @RequestMapping("addto.do")
    public String addtoo(){
        return "add";
    }

    //动态追加课程
    @RequestMapping("listcourse.do")
    @ResponseBody
    //动态追加 也属于集合
    public Object listcourse(){
        List<Course> list=stuService.listcourse();
        return list;
    }

    //添加
    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Stu stu, HttpSession session,
                      Integer[]cids, @RequestParam("photo") MultipartFile photo){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            stu.setPic(upload);
        }
        int i=stuService.add(stu,cids);
        return i;
    }

    @RequestMapping("updato.do")
    public String  updato(){
        return "upda";
    }

    //修改
    @RequestMapping("upda.do")
    @ResponseBody
    public Object upda(Stu stu, HttpSession session,
                      Integer[]cids, @RequestParam("photo") MultipartFile photo){
        if (photo.getSize()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            stu.setPic(upload);
        }
        int i=stuService.upda(stu,cids);
        return i;
    }

    //回显  回的是对象
    @RequestMapping("hui.do")
    @ResponseBody
    public Stu hui(Integer sid){
        Stu s= stuService.hui(sid);
       return s;
    }

}
























